﻿package sprites {
	import astar.AStar;
	import astar.Node;
	import org.flixel.*;
	import org.flixel.data.FlxAnim;

	/**
	 * ...
	 * @author test
	 */
	public class Sheep extends FlxSprite {
		[Embed(source="../../bin/imgs/sheep.png")]
		private var SheepGraphic:Class;
		public var dis:Number;
		public var path:Array;
		public var is_blocked:Boolean = new Boolean(true);
		public var origi_pos:FlxPoint;
		public var _player:Player;
		public var cur_node:uint = new uint(0);

		static public var MAX_RANGE:FlxPoint = new FlxPoint(35 * 1.5, 35 * 1.5);
		static public var MAX_SPEED:Number = 5;
		static public var RAND_DIR_COUNT_MAX:Number = 0.01;
		static public var SENSE_DIS:Number = Math.pow(35 * 0.5, 2);
		public var rand_dir_count:Number = new Number(0);

		override public function update():void {
			super.update();
			if (is_blocked){
				if (Math.pow(_player.x - x, 2) + Math.pow(_player.y - y, 2) <= SENSE_DIS){
					is_blocked = false;
					velocity = new FlxPoint(0, 0);
					return;
				}

				rand_dir_count -= FlxG.elapsed;
				if (rand_dir_count <= 0){
					rand_dir_count = RAND_DIR_COUNT_MAX;
					rand_velocity();
				}

				if (Math.abs(this.x - this.origi_pos.x) >= MAX_RANGE.x){
					this.velocity.x *= -1;
				}
				if (Math.abs(this.y - this.origi_pos.y) >= MAX_RANGE.y){
					this.velocity.y *= -1;
				}
			} else {
				if (cur_node != path.length - 1){
					var cur_loc:FlxPoint = Utils.generalToTile(this.x, this.y);
					//trace(cur_node);
					trace(cur_loc.x, cur_loc.x);
					trace(path[cur_node + 1].x, path[cur_node + 1].y);
					trace("===");
					if (cur_loc.x == path[cur_node + 1].x && cur_loc.y == path[cur_node + 1].y){
						go_along_path();
					}
				}
			}
		}

		public function rand_velocity():void {
			var r:Number = Math.random() * Math.PI * 2;
			var isin:Number = Math.sin(r);
			var icos:Number = Math.cos(Math.PI * 2 - Math.abs(r));
			this.velocity.x = MAX_SPEED * icos;
			this.velocity.y = MAX_SPEED * isin;
		}

		public function go_along_path():void {
			//trace("Go along");
			//if (cur_node != path.length - 1){
			var cn:Node = path[cur_node];
			var nn:Node = path[cur_node + 1];

			
			var dir:uint = AStar.chooseDir(cn, nn);
			velocity.x = AStar.DIR[dir][0] * MAX_SPEED;
			velocity.y = AStar.DIR[dir][1] * MAX_SPEED;
		
			/*
			var a:Number = x - _player.x;
			var b:Number = y - _player.y;
		 	var c:Number = Math.sqrt(a * a + b * b);
			
			
			var angle:Number = FlxU.getAngle(_player.x, _player.y);
			trace(angle);
			velocity.x = Utils.icos(angle) * MAX_SPEED;
			velocity.y = Utils.isin(angle) * MAX_SPEED;
			*/
			cur_node++;
			//}
		}

		public function Sheep(X:Number = 0, Y:Number = 0){
			super(X, Y, SheepGraphic);
			origi_pos = new FlxPoint(X, Y);
			origin = new FlxPoint(0, 0);
		}

		static public function compare(a:Sheep, b:Sheep):Number {
			if (a.dis < b.dis)
				return -1;
			if (a.dis > b.dis)
				return 1;
			return 0;
		}

	}

}